﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ChessOnlineTool
{
    class ChessEngine: IDisposable
    {

        public string EngineName
        {
            get { return System.IO.Path.GetFileNameWithoutExtension(Proc.StartInfo.FileName); }
        }
        public Process Proc;

        public ChessEngine(string filename)
        {
            Proc = new System.Diagnostics.Process();
            Proc.StartInfo.CreateNoWindow = true;
            Proc.StartInfo.FileName = filename;
            Proc.StartInfo.RedirectStandardOutput = true;
            Proc.StartInfo.UseShellExecute = false;
            Proc.StartInfo.RedirectStandardInput = true;
            Proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

        }

        private bool _started = false;
        public bool IsStarted
        {
            get { return _started; }
        }

        public void Start()
        {
            Proc.Start();
            Proc.BeginOutputReadLine();
            _started = true;
        }
        public void Stop()
        {
            Proc.CancelOutputRead();
            Proc.Kill();
            _started = false;
        }

        public override string ToString()
        {
            string s = (IsStarted ? "@" : "");
            return s + EngineName;
        }

        void IDisposable.Dispose()
        {
            Proc.Kill();
        }
    }
}
